﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using System.Data;

namespace SES.CMS.AdminCP
{
    public partial class SendMails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadMail();
        }

        private void LoadMail()
        {

            cmsCustomerBL customerBL = new cmsCustomerBL();
            cmsSendEmailBL emailBL = new cmsSendEmailBL();

            DataTable tbUser = customerBL.SelectAll();
            DataTable tbEmail = emailBL.SelectAll();
            if (tbUser.Rows.Count > 0)
            {
                foreach (DataRow dr in tbUser.Rows)
                {
                    string email = dr["Email"].ToString();
                    string name = dr["FullName"].ToString();
                    ListItem o = new ListItem("<b>" + email + " (" + name + ")</b>", email);
                    checkListEmail.Items.Add(o);
                }
            }
            if (tbEmail.Rows.Count > 0)
            {
                foreach (DataRow dr in tbEmail.Rows)
                {
                    string email = dr["Email"].ToString();
                    ListItem o = new ListItem(email, email);
                    checkListEmail.Items.Add(o);
                }
            }
        }
        protected void checkAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkListEmail.Items.Count; i++)
            {
                checkListEmail.Items[i].Selected = true;
            }
        }
        protected void uncheckAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkListEmail.Items.Count; i++)
            {
                checkListEmail.Items[i].Selected = false;
            }
        }
        protected void checkMember_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkListEmail.Items.Count; i++)
            {
                if (checkListEmail.Items[i].Text.Contains("("))
                {
                    checkListEmail.Items[i].Selected = true;
                }
            }
        }

        protected void checkEmail_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkListEmail.Items.Count; i++)
            {
                if (!checkListEmail.Items[i].Text.Contains("("))
                {
                    checkListEmail.Items[i].Selected = true;
                }
            }
        }

        string MailUser = "contact.topbike@gmail.com";
        string Password = "topbike@123";
        protected void btnSend_Click(object sender, EventArgs e)
        {
            List<string> listEmail = new List<string>();
            for (int i = 0; i < checkListEmail.Items.Count; i++)
            {
                if (checkListEmail.Items[i].Selected)
                {
                    listEmail.Add(checkListEmail.Items[i].Value);
                }
            }
            if (listEmail.Count == 0)
            {
                Myfunction.ThongBao(ltThongBao, "Phải chọn ít nhất 1 Email để gửi !");
                return;
            }
            if (txtTitle.Text == "")
            {
                Myfunction.ThongBao(ltThongBao, "Nhập tiêu đề");
                return;
            }
            if (txtDetail.Content == "")
            {
                Myfunction.ThongBao(ltThongBao, "Nhập nội dung");
                return;
            }
            string toAddress = "";
            foreach (var item in listEmail)
            {
                toAddress += item + ",";
            }
            toAddress = toAddress.Substring(0, toAddress.Length - 1);
            //MyConfig.LoadMail();
            bool ok = Myfunction.SendMail(MailUser, Password, "Admin - TopBike", toAddress, txtTitle.Text, txtDetail.Content);
            if (ok == true)
            {
                Myfunction.ThongBao(ltThongBao, "Gửi mail thành công");
                Reset();
            }
            else
                Myfunction.ThongBao(ltThongBao, "Xảy ra lỗi trong quá trình gửi mail");
        }

        void Reset()
        {
            txtTitle.Text = "";
            txtDetail.Content = "";
        }
    }
}